﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using AutoMapper;
using SAKS.Models;
using SAKS.ViewModels;

namespace SAKS
{
    class OrganizationsToStringArray : ValueResolver<ProjectCreateUpdateModel, ICollection<string>>
    {
        protected override ICollection<string> ResolveCore(ProjectCreateUpdateModel source)
        {
            return source.Organizations.Split(new char[] { ',', ';' });
        }
    }
    class MembersToStringArray:ValueResolver<ProjectCreateUpdateModel,ICollection<string>>
    {
        protected override ICollection<string> ResolveCore(ProjectCreateUpdateModel source)
        {
            return source.Members.Split(new char[] { ',', ';' });
        }
    }
    class StringArrayToOrganizations : ValueResolver<Project,string>
    {
        protected override string ResolveCore(Project source)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var item in source.Organizations)
                sb.Append(item+",");
            return sb.ToString();
        }
    }

    public static class ProjectMapper
    {
        public static Project ToProject(ProjectCreateUpdateModel model)
        {
            Mapper.Reset();
            Mapper.CreateMap<ProjectCreateUpdateModel, Project>()
                    .ForMember(dest => dest.Organizations, opt => opt.ResolveUsing<OrganizationsToStringArray>())
                    .ForMember(dest => dest.Members ,opt => opt.ResolveUsing<MembersToStringArray>())
                    .ForMember(dest => dest.StartDate, opt => opt.MapFrom(p => DateTime.Parse(p.StartDate)))
                    .ForMember(dest => dest.EndDate, opt => opt.MapFrom(p => DateTime.Parse(p.EndDate)));
            var ret = Mapper.Map<ProjectCreateUpdateModel, Project>(model);
            ret.CreatedDate = DateTime.Now;
            return ret;
        }

        public static ProjectCreateUpdateModel ToCreateUpdateModel(Project model)
        {
            Mapper.Reset();
            Mapper.CreateMap<Project, ProjectCreateUpdateModel>()
                .ForMember(dest => dest.Organizations, opt => opt.ResolveUsing<StringArrayToOrganizations>())
                .ForMember(dest => dest.StartDate, opt => opt.MapFrom(s => s.StartDate.Value.ToString("yyyy-MM-dd")))
                .ForMember(dest => dest.EndDate, opt => opt.MapFrom(s => s.EndDate.Value.ToString("yyyy-MM-dd")));
            return Mapper.Map<Project, ProjectCreateUpdateModel>(model);
        }
        public static IList<ProjectListModel> ToListModel(IList<Project> projects)
        {
            Mapper.Reset();
            Mapper.CreateMap<Project, ProjectListModel>()
                .ForMember(dest => dest.CreatedDate, opt => opt.MapFrom(s => s.CreatedDate.ToString("yyyy-MM-dd")));
            var lists = new List<ProjectListModel>();
            foreach (var project in projects)
            {
                lists.Add(Mapper.Map<Project, ProjectListModel>(project));
            }
            return lists;
        }

        public static ProjectDetailsModel ToDetailsModel(Project project)
        {
            Mapper.Reset();
            Mapper.CreateMap<Project, ProjectDetailsModel>()
                .ForMember(dest => dest.StartDate, opt => opt.MapFrom(s => s.StartDate.Value.ToString("yyyy-MM-dd")))
                .ForMember(dest => dest.EndDate, opt => opt.MapFrom(s => s.EndDate.Value!=null?s.EndDate.Value.ToString("yyyy-MM-dd"):"进行中"))
                .ForMember(dest => dest.CreateDate, opt => opt.MapFrom(s => s.CreatedDate.ToString("yyyy-MM-dd")))
                .ForMember(dest => dest.State,opt => opt.MapFrom(s => s.State.ToString()));
            return Mapper.Map<Project, ProjectDetailsModel>(project);
        }


    }
    class PersonInfoGenderResolver : ValueResolver<PersonInfoCreateUpdateModel, Gender>
    {
        protected override Gender ResolveCore(PersonInfoCreateUpdateModel source)
        {
            return source.Gender == "Male" ? Gender.Male : Gender.Female;
        }
 
    }
    
    public static class PersonInfoMapper
    {
        public static PersonInfo ToPersonInfo(PersonInfoCreateUpdateModel source)
        {
            Mapper.Reset();
            Mapper.CreateMap<PersonInfoCreateUpdateModel, PersonInfo>()
                .ForMember(p => p.Gender, opt => opt.ResolveUsing<PersonInfoGenderResolver>())
                .ForMember(p => p.BirthDay,opt => opt.MapFrom( s => DateTime.Parse(s.BirthDay) as DateTime?));

            return Mapper.Map<PersonInfoCreateUpdateModel, PersonInfo>(source);
        }

        public static PersonInfoCreateUpdateModel ToCreateUpdateModel(PersonInfo source)
        {
            Mapper.Reset();
            Mapper.CreateMap<PersonInfo, PersonInfoCreateUpdateModel>()
                .ForMember(p => p.Gender, opt => opt.MapFrom(s => s.Gender.ToString()))
                .ForMember(p => p.BirthDay, opt => opt.MapFrom(s => s.BirthDay.Value.ToString("yyyy-MM-dd")));
            return Mapper.Map<PersonInfo, PersonInfoCreateUpdateModel>(source);
        }
    }

    public static class DocumentMapper
    {
        public static DocumentInfo ToDocumentInfo(DocumentCreateUpdateModel source)
        {
            Mapper.Reset();
            Mapper.CreateMap<DocumentCreateUpdateModel, DocumentInfo>()
                .ForMember(p => p.SecurityLevel, opt => opt.MapFrom(s => Enum.Parse(typeof(SecurityLevel), s.SecurityLevel)))
                .ForMember(p => p.ArchivedDate, opt => opt.MapFrom(s => s.ArchivedDate != null ? DateTime.Parse(s.ArchivedDate) as DateTime? : null))
                .ForMember(p => p.CompletedDate, opt => opt.MapFrom(s => s.CompletedDate != null ? DateTime.Parse(s.CompletedDate) as DateTime? : null));

            return Mapper.Map<DocumentCreateUpdateModel, DocumentInfo>(source);
        }
    }

    public static class ReportMapper
    {
        public static ConferenceReportInfo ToReportInfo(ReportCreateUpdateModel source)
        {
            Mapper.Reset();
            Mapper.CreateMap<ReportCreateUpdateModel, ConferenceReportInfo>();
            return Mapper.Map<ReportCreateUpdateModel, ConferenceReportInfo>(source);
        }
    }

    public static class AchiMapper
    {
        public static AchiViewModel ToViewModel(Achievement source)
        {
            Mapper.Reset();
            Mapper.CreateMap<Achievement, AchiViewModel>()
                .ForMember(a => a.EndDate, opt => opt.MapFrom(s => s.EndDate != null ? s.EndDate.Value.ToString("yyyy-MM-dd") : null))
                .ForMember(a => a.StartDate, opt => opt.MapFrom(s => s.StartDate != null ? s.StartDate.Value.ToString("yyyy-MM-dd") : null))
                .ForMember(a => a.EvaluateDate, opt => opt.MapFrom(s => s.Evaluation.EvaluateDate != null ? s.Evaluation.EvaluateDate.Value.ToString("yyyy-MM-dd") : null))
                .ForMember(a => a.EvaluateLevel, opt => opt.MapFrom(s => s.Evaluation.EvaluateLevel))
                .ForMember(a => a.EvaluateMethod, opt => opt.MapFrom(s => s.Evaluation.EvaluateMethod))
                .ForMember(a => a.EvaluateNumber, opt => opt.MapFrom(s => s.Evaluation.EvaluateNumber))
                .ForMember(a => a.EvaluateOpinion, opt => opt.MapFrom(s => s.Evaluation.EvaluateOpinion))
                .ForMember(a => a.EvaluatePassDate, opt => opt.MapFrom(s => s.Evaluation.EvaluatePassDate != null ? s.Evaluation.EvaluatePassDate.Value.ToString("yyyy-MM-dd") : null));
            return Mapper.Map<Achievement, AchiViewModel>(source);
        }
        public static Achievement ToAchievement(AchiViewModel source)
        {
            Mapper.Reset();
            Mapper.CreateMap<AchiViewModel, Achievement>()
                .ForMember(a => a.EndDate, opt => opt.MapFrom(s => s.EndDate != null ? ((DateTime.Parse(s.EndDate) as DateTime?)):null))
                .ForMember(a => a.StartDate, opt => opt.MapFrom(s => s.StartDate != null ?((DateTime.Parse(s.StartDate) as DateTime?)) : null));
            return Mapper.Map<AchiViewModel, Achievement>(source);
        }
        public static AchiEvaluation ToEvaluation(AchiViewModel source)
        {
            Mapper.Reset();
            Mapper.CreateMap<AchiViewModel, AchiEvaluation>()
                .ForMember(a => a.EvaluatePassDate, opt => opt.MapFrom(s => s.EvaluatePassDate != null ? ((DateTime.Parse(s.EvaluatePassDate) as DateTime?)):null))
                .ForMember(a => a.EvaluateDate, opt => opt.MapFrom(s => s.EvaluateDate !=null ? ((DateTime.Parse(s.EvaluateDate)) as DateTime?):null));

            return Mapper.Map<AchiViewModel, AchiEvaluation>(source);
        }

    }
}